package com.neusoft.service.impl;

import com.neusoft.core.CommonResult;
import com.neusoft.core.PointLog;
import com.neusoft.core.User;
import com.neusoft.mapper.PointLogMapper;
import com.neusoft.mapper.UserMapper;
import com.neusoft.service.PointService;
import org.checkerframework.checker.units.qual.A;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import java.util.List;

@Service
public class PointServiceImpl implements PointService {

	@Autowired
	private PointLogMapper pointLogMapper;
	@Autowired
	private UserMapper userMapper;


	@Override
	public PointLog getPointLogById(Integer logId) {
		return pointLogMapper.getPointLogById(logId);
	}

	@Override
	public List<PointLog> getAllPointLogsByUserId(String userId) {
		List<PointLog> logs=pointLogMapper.getAllPointLogsByUserId(userId);
		if(logs==null){
			return null;
		}
		return logs;
	}

	@Override
	public int addPointLog(PointLog pointLog) {

		//有新积分流水插入的时候更新user对应的积分余额
		User user=userMapper.getUserInfo(pointLog.getUserId());
		userMapper.updatePoints(pointLog.getChange() + user.getPoints(), user.getUserId());

		return pointLogMapper.addPointLog(pointLog);
	}

	@Override
	public int deletePointLogById(Integer logId) {
		return pointLogMapper.deletePointLogById(logId);
	}

}
